$(document).ready(function() {
    var cant=0;
    setInterval(function(){
        $.ajax({
            type: "GET",
            url: HOST_PATHS+'gateway/run',
            dataType: "json",
            success: function(data){
                if(data.c>0){
                    cant+=data.c;
                }
                var totalInbox=0;
                var totalPOSITIVE=0;
                var totalNEGATIVE=0;
                var totalOutbox=0;
                var totalSentitems=0;
                var outboxStatus;
                outboxStatus='<div>Cola de Procesos</div>';
                outboxStatus+='<table class="tablesorter">';
                outboxStatus+='<thead>';
                outboxStatus+='<tr>';
                outboxStatus+='<th>Dispositivo</th>';
                outboxStatus+='<th>Recibido</th>';
                outboxStatus+='<th>Positivo</th>';
                outboxStatus+='<th>Negativo</th>';
                outboxStatus+='<th>Cola</th>';
                outboxStatus+='<th>Enviado</th>';
                outboxStatus+='</tr>';
                outboxStatus+='</thead>';
                outboxStatus+='<tbody>';
                for (var i=1;i<=12;i++){
                    outboxStatus+='<tr>';
                    outboxStatus+='<td>';
                    outboxStatus+='mun'+i;
                    outboxStatus+='</td>';

                    outboxStatus+='<td>';
                    if(data.inbox['mun'+i]){
                        outboxStatus+=data.inbox['mun'+i];
                        totalInbox+=parseInt(data.inbox['mun'+i]);
                    }else{
                        outboxStatus+=0;
                    }
                    outboxStatus+='</td>';

                    outboxStatus+='<td>';
                    if(typeof data.respuesta['mun'+i] != "undefined" && data.respuesta['mun'+i].POSITIVE != null){
                        outboxStatus+=data.respuesta['mun'+i].POSITIVE;
                        totalPOSITIVE+=parseInt(data.respuesta['mun'+i].POSITIVE);
                    }else{
                        outboxStatus+=0;
                    }
                    outboxStatus+='</td>';

                    outboxStatus+='<td>';
                    if(typeof data.respuesta['mun'+i] != "undefined" && data.respuesta['mun'+i].NEGATIVE != null){
                        outboxStatus+=data.respuesta['mun'+i].NEGATIVE;
                        totalNEGATIVE+=parseInt(data.respuesta['mun'+i].NEGATIVE);
                    }else{
                        outboxStatus+=0;
                    }
                    outboxStatus+='</td>';

                    outboxStatus+='<td>';
                    if(typeof data.outbox  != "undefined" && typeof data.outbox['mun'+i] != "undefined"){
                        outboxStatus+=data.outbox['mun'+i];
                        totalOutbox+=parseInt(data.outbox['mun'+i]);
                    }else{
                        outboxStatus+=0;
                    }
                    outboxStatus+='</td>';

                    outboxStatus+='<td>';
                    if(data.sentitems['mun'+i]){
                        outboxStatus+=data.sentitems['mun'+i];
                        totalSentitems+=parseInt(data.sentitems['mun'+i]);
                    }else{
                        outboxStatus+=0;
                    }
                    outboxStatus+='</td>';
                    outboxStatus+='</tr>';
                }
                outboxStatus+='</tbody>';
                outboxStatus+='<tfoot style="font-weight: bold;">';
                outboxStatus+='<tr>';
                outboxStatus+='<td>TOTALES</td>';
                outboxStatus+='<td>'+totalInbox+'</td>';
                outboxStatus+='<td>'+totalPOSITIVE+'</td>';
                outboxStatus+='<td>'+totalNEGATIVE+'</td>';
                outboxStatus+='<td>'+totalOutbox+'</td>';
                outboxStatus+='<td>'+totalSentitems+'</td>';
                outboxStatus+='</tr>';
                outboxStatus+='</tfoot>';
                outboxStatus+='</table>';
                $('#cant').text(cant);
                $('#cant').append(outboxStatus);
            }
        });
    },2000);
})